package com.peng.mybatis.reflection.wrapper;

import com.peng.mybatis.reflection.MetaObject;
import com.peng.mybatis.reflection.property.PropertyTokenizer;

import java.util.List;
import java.util.Map;

public abstract class BaseWrapper implements ObjectWrapper{

    protected static final Object[] NO_ARGUMENTS = {};

    protected MetaObject metaObject;

    protected BaseWrapper(MetaObject metaObject) {
        this.metaObject = metaObject;
    }

    protected Object resolveCollection(PropertyTokenizer prop, Object object) {
        if ("".equals(prop.getName())) {
            return object;
        }
        return metaObject.getValue(prop.getName());
    }

    /**
     * 取集合的值
     * 中括号有2个意思，一个是Map，一个是List或数组
     */
    @SuppressWarnings("all")
    protected Object getCollectionValue(PropertyTokenizer prop, Object collection) {
        if (collection instanceof Map) {
            //map['name']
            return ((Map) collection).get(prop.getIndex());
        } else {
            int i = Integer.parseInt(prop.getIndex());
            return switch (collection) {
                case List list -> list.get(i);
                case Object[] objects -> objects[i];
                case char[] chars -> chars[i];
                case boolean[] booleans -> booleans[i];
                case byte[] bytes -> bytes[i];
                case double[] doubles -> doubles[i];
                case float[] floats -> floats[i];
                case int[] ints -> ints[i];
                case long[] longs -> longs[i];
                case short[] shorts -> shorts[i];
                case null, default ->
                        throw new RuntimeException("The '" + prop.getName() + "' property of "
                                + collection + " is not a List or Array.");
            };
        }
    }

    /**
     * 设集合的值
     * 中括号有2个意思，一个是Map，一个是List或数组
     */
    @SuppressWarnings("all")
    protected void setCollectionValue(PropertyTokenizer prop, Object collection, Object value) {
        if (collection instanceof Map) {
            ((Map) collection).put(prop.getIndex(), value);
        } else {
            int i = Integer.parseInt(prop.getIndex());
            switch (collection) {
                case List list -> list.set(i, value);
                case Object[] objects -> objects[i] = value;
                case char[] chars -> chars[i] = (Character) value;
                case boolean[] booleans -> booleans[i] = (Boolean) value;
                case byte[] bytes -> bytes[i] = (Byte) value;
                case double[] doubles -> doubles[i] = (Double) value;
                case float[] floats -> floats[i] = (Float) value;
                case int[] ints -> ints[i] = (Integer) value;
                case long[] longs -> longs[i] = (Long) value;
                case short[] shorts -> shorts[i] = (Short) value;
                case null, default ->
                        throw new RuntimeException("The '" + prop.getName() + "' property of "
                                + collection + " is not a List or Array.");
            }
        }
    }

}
